import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/:abc*',
      redirect: '/homeMain'
    },
    {
      path:'/login',
      name:'login',
      meta:{
        title:'登录'
      },
      component:()=>import('../views/LoginView.vue')
    },
    {
      path:'/home',
      name:'home',
      meta:{
        title:'任务管理页'
      },
      component:()=>import('../views/HomeView.vue'),
      children:[
        {
          path: '/:abc*',
          redirect: '/homeMain'
        },
        {
          path:'/homeMain',
          name:'homeMain',
          meta:{
            title:'主页面'
          },
          component:()=>import('../views/HomeMainView.vue'),
        },
        {
          path:'/create',
          name:'create',
          meta:{
            title:'新建任务'
          },
          component:()=>import('../views/CreateView.vue')
        },
        {
          path:'/edit',
          name:'edit',
          meta:{
            title:'管理任务'
          },
          component:()=>import('../views/EditView.vue')
        },
        {
          path:'/myTask',
          name:'myTask',
          meta:{
            title:'我的任务'
          },
          component:()=>import('../views/MyTaskView.vue')
        },
        {
          path:'/personnel',
          name:'personnel',
          meta:{
            title:'人员'
          },
          component:()=>import('../views/PersonnelView.vue')
        },
        {
          path:'/calendar',
          name:'calendar',
          meta:{
            title:'日历'
          },
          component:()=>import('../views/CalendarView.vue')
        },
        {
          path:'/my',
          name:'my',
          meta:{
            title:'个人主页'
          },
          component:()=>import('../views/MyView.vue')
        }
        ,
        {
          path:'/declare',
          name:'declare',
          meta:{
            title:'申报BUG'
          },
          component:()=>import('../views/DeclareView.vue')
        }
        ,
        {
          path:'/message',
          name:'message',
          meta:{
            title:'我的消息'
          },
          component:()=>import('../views/MessageView.vue')
        }
      ]
    }
  ]
})

router.beforeEach((to,from,next)=>{
  let needTokenPage=['home','homeMain','create','edit','myTask','personnel','calendar','my','declare','message']
    if(!needTokenPage.includes(to.name))return next();
    const token=sessionStorage.getItem('token')
    if(!token){
        ElMessage({
          showClose: true,
          message: '请先登录 X.x',
          type: 'error',
      })
        return next('/login')
    }
    next()
})
router.afterEach((to,from)=>{
  document.title=to.meta.title||'科加斯任务管理'//设置网页标题
})

export default router
